package com.csun.cmny.provider.task;

import com.csun.cmny.provider.model.domain.DmcDeviceUserBind;
import com.csun.cmny.provider.service.DmcAppSleepDataRecordService;
import com.csun.cmny.provider.service.DmcDeviceUserBindService;
import com.csun.cmny.provider.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Component
public class AppSleepDataQuartz extends QuartzJobBean {

    @Resource
    private DmcAppSleepDataRecordService dmcAppSleepDataRecordService;

    @Resource
    private DmcDeviceUserBindService dmcDeviceUserBindService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(">>>>>>>>>>>>>>>开始生成睡眠数据<<<<<<<<<<<<<<<<<");
        calc();
        log.info(">>>>>>>>>>>>>>>睡眠数据计算完成<<<<<<<<<<<<<<<<<");
    }

    public void calc() {

        String date = DateUtil.dateToStr(new Date());

        List<DmcDeviceUserBind> deviceUserBindList = dmcDeviceUserBindService.selectAll();
        List<Long> deviceIdList = new LinkedList<>();
        for (DmcDeviceUserBind item : deviceUserBindList) {
            deviceIdList.add(item.getDeviceId());
        }

        for(Long deviceId : deviceIdList){
            dmcAppSleepDataRecordService.saveAppSingleDaySleepData(deviceId,date);
        }
    }
}
